<?php
header("Content-Type:text/html; charset=utf-8");
include_once('../inc/init.php');
include_once('inc/adminfun.php');
if(!isAdmin()){
	exit("请登录");
}

$action=strFilter($_GET['action']);
switch($action){
	case "dotopics":
		try{
			$postaction=$_POST['postaction'];
			$tids=$_POST['tids'];
			if(empty($tids)){
				echo("没有选中帖子。");
				return;
			}
			if(is_array($tids)) {
				$tidstr=implode(",",$tids);
				switch($postaction){
					
					case "delPost":
						$attrows=$db->row_select("attachments","tid in ({$tidstr})");
						foreach($attrows as $row){
							$filepath="../uploadfile/attachment/".$row['filepath'];
							if(file_exists($filepath)){
								@unlink($filepath);
							}
							$db->row_delete("attachments","id={$row['id']}");
						}
						$db->row_delete("posts","tid in ({$tidstr})");
						$db->row_delete("topics","id in ({$tidstr})");
						echo("y");
					break;
					
					case "movePost":
						$movetofid=numFilter($_POST['movetofid']);
						$post['fid']=$movetofid;
						$db->row_update("posts",$post,"tid in ({$tidstr})");
						$topic['fid']=$movetofid;
						$db->row_update("topics",$topic,"id in ({$tidstr})");
						echo("y");
					break;	
					
					case "coverPost":
					case "coverPost_":
						$v=($postaction=='coverPost'?1:0);
						$post['iscover']=$v;
						$db->row_update("posts",$post,"tid in ({$tidstr}) and istopic=1");
						echo("y");
					break;
					
					case "lockPost":
					case "lockPost_":
						$v=($postaction=='lockPost'?1:0);
						$topic['islock']=$v;
						$db->row_update("topics",$topic,"id in ({$tidstr})");
						echo("y");
					break;

					case "topPost":
					case "topPost_":
						$v=($postaction=='topPost'?1:0);
						$topic['istop']=$v;
						$db->row_update("topics",$topic,"id in ({$tidstr})");
						echo("y");
					break;

					case "alltopPost":
					case "alltopPost_":
						$v=($postaction=='alltopPost'?1:0);
						$topic['isalltop']=$v;
						$db->row_update("topics",$topic,"id in ({$tidstr})");
						echo("y");
					break;

					case "goodPost":
					case "goodPost_":
						$v=($postaction=='goodPost'?1:0);
						$topic['isgood']=$v;
						$db->row_update("topics",$topic,"id in ({$tidstr})");
						echo("y");
					break;

					case "upPost":
						$topic['ordertime']=time();
						$db->row_update("topics",$topic,"id in ({$tidstr})");
						echo("y");
					break;

					case "stampPost":
						$v=numFilter($_POST['stampid']);
						$topic['stamp']=$v;
						$db->row_update("topics",$topic,"id in ({$tidstr})");
						echo("y");
					break;
					
					case "highlightPost":
						$color=$_POST['highlightfontC'];
						$isb=intval($_POST['highlightfontB']);
						$isi=intval($_POST['highlightfontI']);
						$isu=intval($_POST['highlightfontU']);
						$hlstr='';
						if(!empty($color) || $isb || $isi || $isu){
							$hlstr="{$color},{$isb},{$isi},{$isu}";
						}
						$topic['highlight']=$hlstr;
						$db->row_update("topics",$topic,"id in ({$tidstr})");
						echo("y");
					break;

					case "restorePost":
						$topic['isrecycle']=0;
						$db->row_update("topics",$topic,"id in ({$tidstr})");
						echo("y");
					break;

					default:
						echo"No Such Action";
					break;
				}
			}
		}catch(Exception $e){
			echo($e);
		}
	break;

	/**savedirtywords**/
	case"savedirtywords":
		try{
			$dirtywords_delid=$_POST['dirtywords_delid'];

			//DELETE
			if(is_array($dirtywords_delid)) {
				$delids=implode(",",$dirtywords_delid);
				$db->row_delete("dirtywords","id in ($delids)");
			}

			//UPDATE
			$dirtywords_word=$_POST['dirtywords_word'];
			$dirtywords_replaceto=$_POST['dirtywords_replaceto'];
			$dirtywords_doaction=$_POST['dirtywords_doaction'];

			if(is_array($dirtywords_word)) {
				foreach($dirtywords_word as $key=>$value){
					$dwobj['word']=$dirtywords_word[$key];
					$dwobj['replaceto']=$dirtywords_replaceto[$key];
					$dwobj['doaction']=$dirtywords_doaction[$key];
					$dwobj['type']=0;
					$db->row_update("dirtywords",$dwobj,"id={$key}");
				}
			}

			//INSERT
			$newdirtywords_word=$_POST['newdirtywords_word'];
			$newdirtywords_replaceto=$_POST['newdirtywords_replaceto'];
			$newdirtywords_doaction=$_POST['newdirtywords_doaction'];

			if(is_array($newdirtywords_word)) {
				foreach($newdirtywords_word as $key=>$value){
					$dwobj['word']=$newdirtywords_word[$key];
					$dwobj['replaceto']=$newdirtywords_replaceto[$key];
					$dwobj['doaction']=$newdirtywords_doaction[$key];
					$dwobj['type']=0;
					$db->row_insert("dirtywords",$dwobj);
				}
			}

			echo("y");
		}catch(Exception $e){
			echo($e);
		}
	break;
	
						
	case "clean":
		$db->row_delete("posts","tid in (select id from `{$db->pre}topics` where isrecycle=1)");
		$db->row_delete("topics","isrecycle=1");
		echo("y");
	break;


	case "verifytopic":
		try{
			$doaction=$_POST['doaction'];
			if(is_array($doaction)) {
				foreach($doaction as $id => $value) {
					$value=numFilter($value);

					if($value==1){
						//审核通过
						$topic['verifystate'] = 0;
						$db->row_update("topics",$topic,"id={$id}");
						$post['verifystate'] = 0;
						$db->row_update("posts",$post,"tid={$id} and istopic=1");
					}elseif($value==2){
						//删除
						$attrows=$db->row_select("attachments","tid={$id}");
						foreach($attrows as $row){
							$filepath="../uploadfile/attachment/".$row['filepath'];
							if(file_exists($filepath)){
								@unlink($filepath);
							}
							$db->row_delete("attachments","id={$row['id']}");
						}

						$db->row_delete("posts","tid={$id}");
						$db->row_delete("topics","id={$id}");
					}else{
						//忽略
					}
					
				}
			}
			echo("y");
		}catch(Exception $e){
			echo($e);
		}
	break;

	case "verifypost":
		try{
			$doaction=$_POST['doaction'];
			if(is_array($doaction)) {
				foreach($doaction as $id => $value) {
					$value=numFilter($value);
					
					if($value==1){
						//审核通过
						$post['verifystate'] = 0;
						$db->row_update("posts",$post,"id={$id}");
					}elseif($value==2){
						//删除
						$attrows=$db->row_select("attachments","postid={$id}");
						foreach($attrows as $row){
							$filepath="../uploadfile/attachment/".$row['filepath'];
							if(file_exists($filepath)){
								@unlink($filepath);
							}
							$db->row_delete("attachments","id={$row['id']}");
						}

						$db->row_delete("posts","id={$id}");
					}else{
						//忽略
					}
					
				}
			}
			echo("y");
		}catch(Exception $e){
			echo($e);
		}
	break;
		
}

?>